/**
 *
 * 倒计时
 *
 * @file_name   Counter.js
 * @version     1.0
 * @author      黄新泽
 * @create      2008-05-06
 * @update      2008-09-27 14:45:42
 */

/**
 * 使用示例 ( 10 分钟倒计时 ):
 * var counter = new Counter ('name', 'id', execute_time, text);
 * 
 * 参数说明: name=计算器名称 id = 层ID, execute_time = 剩余秒数, text = 倒计时完成时显示文字
 * 
 */

var	Counter = Class.create({
	name:null,
		
	state:true,
	leftTime:null,
	timer:null,
	id:null,

	displayFormat :  "%%H%%:%%M%%:%%S%%",
	displayDayFormat :  "%%D%%,%%H%%:%%M%%:%%S%%",
	finishMessage :  '倒计时完成',
	countStepper :  -1,
	leadingZero :  true,


	initialize : function(name, id, execute_time, text) 
	{
		if (window[name])
		{
			window[name].clear();
		}

		if (Object.isUndefined(name))
		{
			console.error('计时器名称不能为空');
			this.state = false;
		}
		else
		{
			this.name = name;			
		}

		if (Object.isUndefined(id))
		{
			console.error('计时器容器Id不能为空');
			this.state = false;
		}
		else
		{
			if ($(id))
			{
				this.id = id;
			}
			else
			{
				console.error('计时器容器为空');
				this.state = false;
			}
		}


		if (Object.isUndefined(execute_time))
		{
			console.error('时间不能为空');
			this.state = false;
		}
		else
		{
			this.leftTime = execute_time;
		}


		if (Object.isUndefined(text))
		{
		}
		else
		{
			this.finishMessage = text;
		}

		this.countStepper = Math.ceil(this.countStepper);
		
		if (0 == this.countStepper)
		{
			console.info('不需要执行');
			this.state = false;
		}

		if (this.state)
		{
			this.setTimeOutPeriod = (Math.abs(this.countStepper) - 1) + 1;

			this.update();
			this.diyUpdate();
			this.timer = new PeriodicalExecuter(this.count.bind(this), this.setTimeOutPeriod); 
		}
	},
	
	formatDate : function(num1, num2)
	{
		s = ((Math.floor(this.leftTime / num1)) % num2).toString();
		if (this.leadingZero && s.length < 2) s = "0" + s;

		return "<b>" + s + "</b>";
	},

	count : function(e)
	{
		//console.info('count');

		if (!$(this.id))
		{
			this.state = false;
		}

		this.leftTime = this.leftTime + this.countStepper;

		if (this.state)
		{
			this.update();
			this.diyUpdate();
		}

		if (!this.state)
		{
			this.clear();
		}


		if (0==this.leftTime || this.leftTime<0)
		{
			this.complete();
			this.clear();

			return ;
		}
	},

	update : function()
	{
		var  display_str;

		if (86400 < this.leftTime)
		{
			display_str = this.displayDayFormat.replace(/%%D%%/g, this.formatDate(86400, 100000));
			display_str = display_str.replace(/%%H%%/g, this.formatDate(3600, 24));
			display_str = display_str.replace(/%%M%%/g, this.formatDate(60, 60));
			display_str = display_str.replace(/%%S%%/g, this.formatDate(1, 60));
		}
		else
		{
			display_str = this.displayFormat.replace(/%%H%%/g, this.formatDate(3600, 24));
			display_str = display_str.replace(/%%M%%/g, this.formatDate(60, 60));
			display_str = display_str.replace(/%%S%%/g, this.formatDate(1, 60));
		}

		$(this.id).update(display_str);
	},

	diyUpdate : function()
	{
	},

	complete : function()
	{
		if (this.finishMessage)
		{
			$(this.id).update(this.finishMessage);
		}
	},

	clear : function()
	{
		try
		{
			this.timer.stop();
			this.timer = null;
		}
		catch(e)
		{}

		window[this.name] = null;
	}
});